package com.ruoyi.project.data.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.data.mapper.WeightResponsibilityNoticeMapper;
import com.ruoyi.project.data.domain.WeightResponsibilityNotice;
import com.ruoyi.project.data.service.IWeightResponsibilityNoticeService;

/**
 * 责任告知书Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-22
 */
@Service
public class WeightResponsibilityNoticeServiceImpl implements IWeightResponsibilityNoticeService 
{
    @Autowired
    private WeightResponsibilityNoticeMapper weightResponsibilityNoticeMapper;

    /**
     * 查询责任告知书
     * 
     * @param id 责任告知书ID
     * @return 责任告知书
     */
    @Override
    public WeightResponsibilityNotice selectWeightResponsibilityNoticeById(Long id)
    {
        return weightResponsibilityNoticeMapper.selectWeightResponsibilityNoticeById(id);
    }

    /**
     * 查询责任告知书列表
     * 
     * @param weightResponsibilityNotice 责任告知书
     * @return 责任告知书
     */
    @Override
    public List<WeightResponsibilityNotice> selectWeightResponsibilityNoticeList(WeightResponsibilityNotice weightResponsibilityNotice)
    {
        return weightResponsibilityNoticeMapper.selectWeightResponsibilityNoticeList(weightResponsibilityNotice);
    }

    /**
     * 新增责任告知书
     * 
     * @param weightResponsibilityNotice 责任告知书
     * @return 结果
     */
    @Override
    public int insertWeightResponsibilityNotice(WeightResponsibilityNotice weightResponsibilityNotice)
    {
        weightResponsibilityNotice.setCreateTime(DateUtils.getNowDate());
        weightResponsibilityNotice.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return weightResponsibilityNoticeMapper.insertWeightResponsibilityNotice(weightResponsibilityNotice);
    }

    /**
     * 修改责任告知书
     * 
     * @param weightResponsibilityNotice 责任告知书
     * @return 结果
     */
    @Override
    public int updateWeightResponsibilityNotice(WeightResponsibilityNotice weightResponsibilityNotice)
    {
//        WeightResponsibilityNotice notice = weightResponsibilityNoticeMapper.selectWeightResponsibilityNoticeById(weightResponsibilityNotice.getId());
//        if (!weightResponsibilityNotice.getOneNoticePath().equals(notice.getOneNoticePath())){
//            FileUtils.deleteFile(RuoYiConfig.getProfile()+ "/"+notice.getOneNoticePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getOneNoticePath().length()));
//        }if (!weightResponsibilityNotice.getTwoNoticePath().equals(notice.getTwoNoticePath())){
//            FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getTwoNoticePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getTwoNoticePath().length()));
//        }if (!weightResponsibilityNotice.getCompanyPath().equals(notice.getCompanyPath())){
//            FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getCompanyPath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getCompanyPath().length()));
//        }if (!weightResponsibilityNotice.getPromisePath().equals(notice.getPromisePath())){
//            FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getPromisePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getPromisePath().length()));
//        }
        weightResponsibilityNotice.setUpdateTime(DateUtils.getNowDate());
        weightResponsibilityNotice.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return weightResponsibilityNoticeMapper.updateWeightResponsibilityNotice(weightResponsibilityNotice);
    }

    /**
     * 批量删除责任告知书
     * 
     * @param ids 需要删除的责任告知书ID
     * @return 结果
     */
    @Override
    public int deleteWeightResponsibilityNoticeByIds(Long[] ids)
    {
        for (Long id : ids){
            WeightResponsibilityNotice notice = weightResponsibilityNoticeMapper.selectWeightResponsibilityNoticeById(id);
            if (StringUtils.isNotBlank(notice.getOneNoticePath())){
                FileUtils.deleteFile(RuoYiConfig.getProfile()+ "/"+notice.getOneNoticePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getOneNoticePath().length()));
            }if (StringUtils.isNotBlank(notice.getTwoNoticePath())){
                FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getTwoNoticePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getTwoNoticePath().length()));
            }if (StringUtils.isNotBlank(notice.getCompanyPath())){
                FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getCompanyPath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getCompanyPath().length()));
            }if (StringUtils.isNotBlank(notice.getPromisePath())){
                FileUtils.deleteFile(RuoYiConfig.getProfile()+"/"+notice.getPromisePath().substring(Constants.RESOURCE_PREFIX.length()+1,notice.getPromisePath().length()));
            }
        }
        return weightResponsibilityNoticeMapper.deleteWeightResponsibilityNoticeByIds(ids);
    }

    /**
     * 删除责任告知书信息
     * 
     * @param id 责任告知书ID
     * @return 结果
     */
    @Override
    public int deleteWeightResponsibilityNoticeById(Long id)
    {
        return weightResponsibilityNoticeMapper.deleteWeightResponsibilityNoticeById(id);
    }
}
